package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.mobile.vo.request.loan.DeleteUserLetterRequestVO;
import com.niiwoo.civet.mobile.vo.request.loan.ReadUserLetterRequestVO;
import com.niiwoo.civet.mobile.vo.request.loan.SearchUserLetterListRequestVO;
import com.niiwoo.civet.mobile.vo.response.QueryUserLetterExtInfoResponseVO;
import com.niiwoo.civet.mobile.vo.response.user.QueryUserLetterVO;
import com.niiwoo.civet.user.dto.request.SearchUserLetterListRequestDTO;
import com.niiwoo.civet.user.dto.response.QueryUserLetterExtInfoResponseDTO;
import com.niiwoo.civet.user.dto.response.QueryUserLetterListResponseDTO;
import com.niiwoo.civet.user.service.UserLetterDubboService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Empty;
import com.niiwoo.tripod.web.vo.PageResponseVO;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 站内信业务
 * @author xueyuke
 * @date 2017-12-25
 */
@RestController
@RequestMapping("/user")
@Api(tags = "站内信", description = "/user")
@Slf4j
public class UserLetterController {
    @Reference(version = "1.0.0")
    UserLetterDubboService userLetterDubboService;

    @PostMapping("/queryLetterList")
    @ApiOperation(value = "查询站内信列表")
    public Result<PageResponseVO<QueryUserLetterVO>> queryLetterList(@RequestBody @Valid SearchUserLetterListRequestVO searchUserLetterListRequestVO) {
        log.info("查询站内信列表,请求参数={}", JSON.toJSONString(searchUserLetterListRequestVO));
        String userId = PrincipalContext.getUserId();
        log.info("查询站内信列表,用户ID={}", userId);
        SearchUserLetterListRequestDTO searchUserLetterListRequestDTO=new SearchUserLetterListRequestDTO();
        BeanUtils.copyProperties(searchUserLetterListRequestVO,searchUserLetterListRequestDTO);
        searchUserLetterListRequestDTO.setUserId(userId);
        log.info("查询站内信列表,用户ID={},请求用户中心查询站内信列表接口参数={}", userId,JSON.toJSON(searchUserLetterListRequestDTO));
        PageResponseDTO<QueryUserLetterListResponseDTO> userLetterList=userLetterDubboService.queryUserLetterList(searchUserLetterListRequestDTO);
        PageResponseVO<QueryUserLetterVO> result=transDTO2VOList(userLetterList);
        log.info("查询站内信列表,响应结果={}", JSON.toJSONString(result));
        return Result.with(result);
    }

    @PostMapping("/queryLetterExtInfo")
    @ApiOperation(value = "查询站内信扩展信息")
    public Result<QueryUserLetterExtInfoResponseVO> queryLetterExtInfo() {
        String userId = String.valueOf(PrincipalContext.getUserId());
        log.info("查询站内信扩展信息,用户ID={},", userId);

        QueryUserLetterExtInfoResponseDTO queryUserLetterExtInfoResponseDTO=userLetterDubboService.queryUserLetterExtInfo(userId);

        QueryUserLetterExtInfoResponseVO responseVO = new QueryUserLetterExtInfoResponseVO();
        BeanUtils.copyProperties(queryUserLetterExtInfoResponseDTO, responseVO);

        if(queryUserLetterExtInfoResponseDTO.getLastUserLetter() != null) {
            responseVO.setLastUserLetter(new QueryUserLetterExtInfoResponseVO.UserLetter());
            BeanUtils.copyProperties(queryUserLetterExtInfoResponseDTO.getLastUserLetter(), responseVO.getLastUserLetter());
        }
        return Result.with(responseVO);
    }

    @PostMapping("/cleanRedPoint")
    @ApiOperation(value = "清除小红点")
    public Empty cleanRedPoint() {
        String userId = String.valueOf(PrincipalContext.getUserId());
        log.info("清除小红点,用户ID={}", userId);
        userLetterDubboService.cleanRedPoint(userId);

        return Empty.getInstance();
    }

    @PostMapping("/readLetter")
    @ApiOperation(value = "已读站内信")
    public Empty readLetter(@RequestBody @Valid ReadUserLetterRequestVO readUserLetterRequestVO) {
        String userId = String.valueOf(PrincipalContext.getUserId());
        log.info("已读站内信,用户ID={},请求参数={}", userId,JSON.toJSONString(readUserLetterRequestVO));
        userLetterDubboService.readLetter(readUserLetterRequestVO.getUserLetterId());

        return Empty.getInstance();
    }

    @PostMapping("/deleteLetter")
    @ApiOperation(value = "删除站内信")
    public Empty deleteLetter(@RequestBody @Valid DeleteUserLetterRequestVO deleteUserLetterRequestVO) {
        String userId = String.valueOf(PrincipalContext.getUserId());
        log.info("删除站内信,用户ID={},请求参数={}", userId,JSON.toJSONString(deleteUserLetterRequestVO));
        userLetterDubboService.deleteLetter(deleteUserLetterRequestVO.getUserLetterIdList());

        return Empty.getInstance();
    }

    private PageResponseVO<QueryUserLetterVO> transDTO2VOList(PageResponseDTO<QueryUserLetterListResponseDTO> queryUserLetterListResponseDTOList){
        PageResponseVO<QueryUserLetterVO> pageResponseDTO=new PageResponseVO<QueryUserLetterVO>();
        List<QueryUserLetterVO> queryUserLetterVOList=new ArrayList<QueryUserLetterVO>();
        for(QueryUserLetterListResponseDTO queryUserLetterListResponseDTO:queryUserLetterListResponseDTOList.getItems()){
            QueryUserLetterVO queryUserLetterVO=new QueryUserLetterVO();
            queryUserLetterVO.setId(queryUserLetterListResponseDTO.getId());
            queryUserLetterVO.setTitle(queryUserLetterListResponseDTO.getTitle());
            queryUserLetterVO.setContent(queryUserLetterListResponseDTO.getContent());
            queryUserLetterVO.setSendDate(queryUserLetterListResponseDTO.getSendDate()==null?"":DateFormatUtils.format(queryUserLetterListResponseDTO.getSendDate(),"yyyy-MM-dd HH:mm:ss"));
            queryUserLetterVO.setUserId(queryUserLetterListResponseDTO.getToUserId());
            queryUserLetterVO.setIsRead(queryUserLetterListResponseDTO.getIsRead());
            queryUserLetterVO.setViewDate(queryUserLetterListResponseDTO.getViewDate()==null?"":DateFormatUtils.format(queryUserLetterListResponseDTO.getViewDate(),"yyyy-MM-dd HH:mm:ss"));
            queryUserLetterVO.setButtonList(queryUserLetterListResponseDTO.getButtonList());

            queryUserLetterVOList.add(queryUserLetterVO);
        }
        pageResponseDTO.setItems(queryUserLetterVOList);
        pageResponseDTO.setTotalCount(queryUserLetterListResponseDTOList.getTotalCount());
        pageResponseDTO.setTotalPage(queryUserLetterListResponseDTOList.getTotalPage());

        return pageResponseDTO;
    }
}
